package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.ruoyi.common.constdef.CONFIG;
import com.ruoyi.common.utils.Cache.VCache;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class generatewxQRcode {



    /**
     * 生成微信图片上传七牛云
     * @param name 图片名称
     * @return
     */
    public static String generateWXQRcodes(String name,String pageUrl,String sence){


        try
        {
            //返回微信字节流
            String s = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";
            String accessToken = VCache.get("wohuituiAccessToken",String.class);
            String codeUrl = String.format(s,accessToken);
            URL url = new URL(codeUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();

            paramJson.put("scene", sence);
            paramJson.put("page", pageUrl);//pages/shop/shopdetails/shopdetails
            paramJson.put("width", 430);
            paramJson.put("auto_color", false);
//            String line_color="{\"r\":121,\"g\":120,\"b\":208}";
//            com.alibaba.fastjson.JSONObject json = com.alibaba.fastjson.JSONObject.parseObject(line_color);
//            paramJson.put("line_color", json);
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();

            //开始获取数据 从微信接收的太阳码流
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            //将太阳码流保存在内存中
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //缓冲2KB
            byte[] buffer = new byte[2 * 1024];
            int len;
            while((len=bis.read(buffer))!=-1){
                //写入到内存中
                baos.write(buffer,0,len);
            }
            //太阳码数据
            byte[] data=baos.toByteArray();



            //创建七牛对象
            Auth auth = Auth.create(CONFIG.QINIU_ACCESSKEY,CONFIG.QINIU_SECRETKEY);
            String token = auth.uploadToken(CONFIG.QINIU_BUCKET);
            String key = name+".jpg";
            Configuration cfg = new Configuration(Zone.zone2());
            UploadManager uploadManager = new UploadManager(cfg);
            try {
                Response response = uploadManager.put(data, key, token);
                String content = response.bodyString();
                DefaultPutRet defaultPutRet = JSON.parseObject(content,DefaultPutRet.class);
                String responseKey = defaultPutRet.key;
                System.out.println(defaultPutRet.key);
                System.out.println(defaultPutRet.hash);
                return responseKey;
            } catch (Exception e){
                return "上传异常";
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 微信搜一搜
     * @param item_code  商品id
     * @param title  物品名称
     * @param desc  物品描述
     * @param className  上级分类关键词
     * @param pclassName  下级分类关键词
     * @param imgURL  图片url
     * @param sku_id  商品id
     * @param price   商品现在价格
     * @param original_price  商品原价
     * @param longitude  经度
     * @param latitude  纬度
     * @param radius  半径
     * @param business_name 店名
     * @param branch_name 分店名字
     * @param address  详细地址
     * @param barcode_type 条形码类型, 目前支持"ean8", "ean13"
     * @param barcode  条形码数字字符串
     * @return
     */
    public static String wxsys(String item_code,String title,String desc,String className,String pclassName,String imgURL,String sku_id
    ,String price,String original_price,String longitude,String latitude,String radius,String business_name,String branch_name
    ,String address,String barcode_type,String barcode){
        String WxAccessToken = VCache.get("WxAccessToken");
        try {
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + WxAccessToken);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
//             conn.setConnectTimeout(10000);//连接超时 单位毫秒
//             conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();

            JSONObject product_list = new JSONObject();
            product_list.put("item_code","商品id");
            product_list.put("title","物品名称");
            product_list.put("desc","物品描述");


            List<Object>  category_list=new ArrayList<>();
            category_list.add("上级分类关键词");
            category_list.add("下级分类关键词");
            product_list.put("category_list",category_list);


            List<Object>  image_list=new ArrayList<>();
            image_list.add("图片url_1");
            product_list.put("image_list",image_list);



            JSONObject sku_list = new JSONObject();
            sku_list.put("sku_id","1");
            sku_list.put("price","10000");
            sku_list.put("original_price","200000");
            sku_list.put("status","1");

            JSONObject poi_list = new JSONObject();
            poi_list.put("longitude","116.32676");
            poi_list.put("latitude","40.003305");
            poi_list.put("radius","0");
            poi_list.put("business_name","老毛烧烤");
            poi_list.put("branch_name","老毛鸭店");
            poi_list.put("address","新港中路123号");
            JSONArray poi_lists = new JSONArray();
            poi_lists.add(0,poi_list);
            product_list.put("poi_list",poi_lists);


            JSONObject bar_code_info = new JSONObject();
            bar_code_info.put("barcode_type","ean8");
            bar_code_info.put("barcode","12345678");
            product_list.put("bar_code_info",bar_code_info);


            JSONArray jsonArray = new JSONArray();
            jsonArray.add(0,product_list);
            paramJson.put("product_list",jsonArray);

            printWriter.write(paramJson.toString());
            printWriter.flush();
            return paramJson.toString();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 测试数据格式
     * @return
     */
    public static String wxsysTest(){

        JSONObject paramJson = new JSONObject();

        JSONObject product_list = new JSONObject();
        product_list.put("item_code","商品id");
        product_list.put("title","物品名称");
        product_list.put("desc","物品描述");


        String category_list1="上级分类关键词";
        String category_list2="下级分类关键词";
        String[] category_list={category_list1,category_list2};
        product_list.put("category_list",category_list);


        List<String>  image_list=new ArrayList<>();
        image_list.add("图片url_1");
        image_list.add("图片url_2");
        product_list.put("image_list",image_list);



        JSONObject sku_list = new JSONObject();
        sku_list.put("sku_id","1");
        sku_list.put("price","10000");
        sku_list.put("original_price","200000");
        sku_list.put("status","1");

        JSONObject poi_list = new JSONObject();
        poi_list.put("longitude","116.32676");
        poi_list.put("latitude","40.003305");
        poi_list.put("radius","0");
        poi_list.put("business_name","肯老毛");
        poi_list.put("branch_name","肯老毛店");
        poi_list.put("address","新港中路123号");
        JSONArray poi_lists = new JSONArray();
        poi_lists.add(0,poi_list);
        product_list.put("poi_list",poi_lists);


        JSONObject bar_code_info = new JSONObject();
        bar_code_info.put("barcode_type","ean8");
        bar_code_info.put("barcode","12345678");
        product_list.put("bar_code_info",bar_code_info);


        JSONArray jsonArray = new JSONArray();
        jsonArray.add(0,product_list);
        paramJson.put("product_list",jsonArray);


        return paramJson.toString();
    }

}
